package servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet 
{
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException 
	{
		doProcessing(req, resp);
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException 
	{
		doProcessing(req, resp);
	}
	
	private void doProcessing(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
	{	
		// get username and password from the form and initialise a string
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		
		// set the previous strings as attributes to get later in the index.jsp
		req.getSession().setAttribute("username", username);
		req.getSession().setAttribute("password", password);
		
		// go back to the index.jsp
		RequestDispatcher dispatcher = req.getRequestDispatcher("index.jsp");
		dispatcher.forward(req, resp);
	}
}
